from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.expressions import OrderBy
from django.db.models.fields.related import ForeignKey
import itsdangerous
from django.conf import settings


class User(AbstractUser):
    """自定义用户模型"""

    mobile = models.CharField(max_length=11, unique=True, verbose_name="手机号")
    email_activated = models.BooleanField(default=False)

    class Meta:
        db_table = "tb_users"
        verbose_name = "用户"
        verbose_name_plural = verbose_name

    @staticmethod
    def get_verify_url(user):
        salt = settings.SECRET_KEY
        t = itsdangerous.TimedJSONWebSignatureSerializer(salt=salt, expires_in=3600, secret_key=salt)
        info = {'username': user.username, 'email': user.email}
        info = t.dumps(info).decode()
        return "http://192.168.0.10:7777/user/emailactivate/" + info


class DeliveryRecord(models.Model):
    '''
    收货地址
    '''
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='delivery_records')
    province = models.OneToOneField('area.Area', on_delete=models.CASCADE, related_name='delivery_province', )
    city = models.OneToOneField('area.Area', on_delete=models.CASCADE, related_name='delivery_city')
    county = models.OneToOneField('area.Area', on_delete=models.CASCADE, related_name='delivery_county')
    detail = models.CharField(max_length=888)
    phone = models.PositiveSmallIntegerField(max_length=11)
    home_phone = models.PositiveSmallIntegerField(max_length=11)
    email = models.EmailField()
